﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Valor.DBUtility.MSSQLHelper;

using System.Data.SqlClient;
using System.Data;
using xTeam.HengLe.Entity;


namespace xTeam.HengLe.DataAccess {
    public class AdvertiseDataAccess {
        private bool _isCN = true;

        public AdvertiseDataAccess(string connectionString, string langName) {
            SQLProcedureHelper.ConnectionString = connectionString;
            this._isCN = langName.ToLower() == "en-us" ? false : true;
        }

        public int Save(EAdvertise ad) {
            int result = 0;
            SqlParameter[] parameters =  
            {
                new SqlParameter("@Description",SqlDbType.NVarChar,3000){Value = ad.Description},
                new SqlParameter("@Aid",SqlDbType.Int){Value = ad.Aid},
                new SqlParameter("@Contents",SqlDbType.NVarChar,3000){Value = ad.Contents},
                new SqlParameter("@ExtendKey",SqlDbType.NVarChar,50){Value = ad.ExtendKey},
                new SqlParameter("@Sequence",SqlDbType.Int){Value = ad.Sequence},
                new SqlParameter("@Type",SqlDbType.Int){Value = ad.Type},
                new SqlParameter("@Title",SqlDbType.NVarChar,50){Value = ad.Title},
                new SqlParameter("@ModuleKey",SqlDbType.NVarChar,50){Value = ad.ModuleKey}
            };

            result = Convert.ToInt32(SQLProcedureHelper.ExecuteScalar(this._isCN ? "Advertise_Save" : "Advertise_Save_en", parameters));
            return result;
        }

        public EAdvertise SelectById(int aid) {
            EAdvertise ad = null;

            SqlDataReader reader = SQLProcedureHelper.ExecuteReader(this._isCN ? "Advertise_SelectById" : "Advertise_SelectById_en", new SqlParameter("@Aid", SqlDbType.Int) { Value = aid });


            if (reader.HasRows) {
                ad = new EAdvertise();
                reader.Read();

                ad.Aid = Convert.ToInt32(reader["Aid"]);
                ad.Contents = reader["Contents"].ToString();
                ad.Description = reader["Description"].ToString();
                ad.ExtendKey = reader["ExtendKey"].ToString();
                ad.ModuleKey = reader["ModuleKey"].ToString();
                ad.Sequence = Convert.ToInt32(reader["Sequence"]);
                ad.Title = reader["Title"].ToString();
                ad.Type = Convert.ToInt32(reader["Type"]);
            }
            reader.Close();
            return ad;
        }

        public List<EAdvertise> SelectList(string extendKey, string moduleKey) {

            List<EAdvertise> adList = new List<EAdvertise>();

            SqlParameter[] parameters = 
            {
                 new SqlParameter("@ExtendKey", SqlDbType.NVarChar,50) { Value = extendKey },
                 new SqlParameter("@ModuleKey",SqlDbType.NVarChar,50){Value = moduleKey}
            };
            SqlDataReader reader = SQLProcedureHelper.ExecuteReader(this._isCN ? "Advertise_SelectList":"Advertise_SelectList_en", parameters);

            while (reader.Read()) {
                adList.Add(new EAdvertise() {
                    Aid = Convert.ToInt32(reader["Aid"]),
                    Contents = reader["Contents"].ToString(),
                    Description = reader["Description"].ToString(),
                    ExtendKey = reader["ExtendKey"].ToString(),
                    ModuleKey = reader["ModuleKey"].ToString(),
                    Sequence = Convert.ToInt32(reader["Sequence"]),
                    Title = reader["Title"].ToString(),
                    Type = Convert.ToInt32(reader["Type"])
                });
            }
            reader.Close();
            return adList;
        }
    }
}
